#pragma once
#include "CardImage.h"
#include "Deck.h"
#include "Game.h"
#include "EventQ.h"
#include "PropChangeSupport.h"
#include "TurnPlay.h"
namespace SHGame {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace SHGame;
	using namespace SHGame::Events;


	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this ref class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this ref class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		static Form1^ Instance;
	public:
		int gamenum;
		CardSet^ cardset;
		int mouseover;
		PropChangeSupport^ pcs;
		Game^ game;
		bool waiting;
		bool repaint_now;
	private: Dictionary<int, Card^> ^playedCards;

	private: System::Windows::Forms::Panel^  handPanel7;
	private: System::Windows::Forms::Panel^  handPanel6;
	private: System::Windows::Forms::Panel^  panel3;
	private: System::Windows::Forms::Panel^  player4_CardPanel;
	private: System::Windows::Forms::Panel^  player5_cardPanel;
	private: System::Windows::Forms::Panel^  player2_cardPanel;
	private: System::Windows::Forms::Panel^  player3_cardPanel;
	private: System::Windows::Forms::Panel^  human_cardPanel;
	private: System::Windows::Forms::Label^  player3_label;
	private: System::Windows::Forms::GroupBox^  picker_groupBox;
	private: System::Windows::Forms::Button^  pass_button;
	private: System::Windows::Forms::Button^  pickup_button;
	private: System::Windows::Forms::Label^  player2_label;
	private: System::Windows::Forms::Label^  player5_label;
	private: System::Windows::Forms::Label^  player4_label;
	private: System::Windows::Forms::Timer^  timer1;

	private:
		Deck ^deck;
	private: System::Windows::Forms::RichTextBox^  richTextBox1;
	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	private: System::Windows::Forms::ToolStripMenuItem^  fileToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  exitToolStripMenuItem;
	private: System::Windows::Forms::Label^  clickToCont;

	public:
		void postMsg(String^ str) {
			this->Invoke(gcnew StringDelegate(this, &Form1::delegatePrompt), str);
		}

			 array<Label^>^ nameTags;
		delegate void SetText(Label^ l, String^ s);
		delegate void Repaint();
		delegate void HandleDel(Object^, PropChangeEvent^);
		delegate void SetPicking(bool);
		delegate void StringDelegate(String^);
	private:
		
		/*
		A method to post a string to the text-box
		*/
		void PostMessage(String^ str) {
			this->richTextBox1->AppendText("\n"+str);
		}

		
		/*
		Thread safe Post Message to text box
		*/
		void InvokePostMessage(String^ str) {
			this->Invoke( gcnew StringDelegate(this, &Form1::PostMessage), str );
		}


		void setLabelText(Label^ l, String^ str) {
			l->Text = str;
			if (str->Contains("pass"))
				l->ForeColor = Color::Gray;
			else if (str->Contains("pick"))
				l->ForeColor = Color::Red;
			else
				l->ForeColor = Color::Black;
		}
		void handle(System::Object^ o, PropChangeEvent^ args) {
			this->Invoke( gcnew Repaint(this, &Form1::Refresh));
		}
		void delegatePrompt(String^msg) {
			this->human_label->Text = msg;
		}
		void handlePass(System::Object^ o, PropChangeEvent^ args) {
			Player^ p = (Player^) args->New;
			InvokePostMessage(p->ToString() + " passes.");
			int i = TurnPlay::GetIndexOf(p);
			if (i < 1)
				return;
			Label^ l = nameTags[i-1];
			l->Invoke(gcnew SetText(this, &Form1::setLabelText), l, (p->ToString() + " pass"));
			
		}
		void handleHumanChoice(System::Object^ o, PropChangeEvent^ args)
		{
			this->Invoke(gcnew StringDelegate(this, &Form1::delegatePrompt), "Select Pick or Pass");
			if (o == nullptr) {
				picker_groupBox->Enabled = true;
				pickup_button->Enabled = true;
				pass_button->Enabled = true;
			}
			else
				this->Invoke(gcnew SetPicking(this, &Form1::setPicking), true);
		}

		void setPicking(bool b) {
		
				picker_groupBox->Enabled = b;
				pickup_button->Enabled = b;
				pass_button->Enabled = b;
		}

		/*
		Game Over Handler
		*/
		void GameOver(System::Object^ o, PropChangeEvent^ args) {
			InvokePostMessage("Game over!");
			this->Invoke(gcnew StringDelegate(this, &Form1::delegatePrompt), "Click the Next button to play the next game");
			if (game->Picker != nullptr) {
				// if we have a picker
				if (game->PickerPoints > 60) {
					// picker wins!
					InvokePostMessage("Picker wins with" + game->PickerPoints);
					InvokePostMessage("Winners:" + game->Picker + " and " + game->Partener);
				} else {
					InvokePostMessage("Picker loses with" + game->PickerPoints);
					InvokePostMessage("losers: " + game->Picker + " and " + game->Partener);
				}
			} else {
				// leaster
				Player^ low = nullptr;
				int lo = 121;
				for (int i = 0; i < 5; i ++) {
					Player^ t = TurnPlay::GetPlayerAt(i);
					if (!game->TookTrick(t))
						continue;
					int ti = game->GetPoints(t);
					if (ti < lo) {
						lo = ti;
						low = t;
					}
				}
				InvokePostMessage("Leaster winning: "+low+" with "+lo+" points");
			}
			InvokePostMessage("\n");
			
 			this->Invoke( gcnew SetPicking(this, &Form1::setEnabled), true );
		}

		void setEnabled(bool b) {
			this->button1->Enabled = b;
		}

		void handlePicking(System::Object^ o, PropChangeEvent^ args) {
			Player^ p = (Player^) args->New;
			InvokePostMessage(p + " picks.");
			int i = TurnPlay::GetIndexOf(p);
			if (i < 1) {
				Diagnostics::Debug::WriteLine(" Skipping pick event");
				return;
			}
			Diagnostics::Debug::WriteLine(" using pick event on player # " + i);
			Label^ l = nameTags[i-1];
			l->Invoke(gcnew SetText(this, &Form1::setLabelText), l, (p->ToString() + " pick"));
		}

		void handleReqHumanQ(System::Object^ o, PropChangeEvent^ args) {
			if (! clickToCont->Visible)
				this->Invoke(gcnew Repaint(this->clickToCont, &Label::Show));
			this->Invoke(gcnew StringDelegate(this, &Form1::delegatePrompt), "Click the center of the trick to continue");
		}
	public:	
		/*

		CONSTRUCTOR

		*/
		Form1(void)
		{
			Form1::Instance = this;
			repaint_now = false;
			pcs = gcnew PropChangeSupport(this);
			EventHandler<PropChangeEvent^>^ handler = gcnew EventHandler<PropChangeEvent^>(this, &Form1::handle);
			EventQ::AddListener("repaint", handler);

			EventHandler<PropChangeEvent^>^ handler2 = gcnew EventHandler<PropChangeEvent^>(this, &Form1::handlePass);
			EventQ::AddListener("pass", handler2);

			EventHandler<PropChangeEvent^>^ handler3 = gcnew EventHandler<PropChangeEvent^>(this, &Form1::handleHumanChoice);
			EventQ::AddListener("human.pickpass", handler3);

			EventHandler<PropChangeEvent^>^ handler4 = gcnew EventHandler<PropChangeEvent^>(this, &Form1::handlePicking);
			EventQ::AddListener("pick", handler4);

			EventHandler<PropChangeEvent^>^ handler5 = gcnew EventHandler<PropChangeEvent^>(this, &Form1::GameOver);
			EventQ::AddListener("gameover", handler5);
			
			EventQ::AddListener("reqhumanq", gcnew EventHandler<PropChangeEvent^>(this, &Form1::handleReqHumanQ));

			TurnPlay::Start();


			waiting = false;
			InitializeComponent();
			array<Label^>^ temparr = { player2_label, player3_label, player4_label, player5_label };
			nameTags = temparr;
			for (int i = 1; i < 5; i++) {
				nameTags[i-1]->Text = TurnPlay::GetPlayerAt(i)->name;
				setLabelText(nameTags[i-1], TurnPlay::GetPlayerAt(i)->name);
			}
			//
			//TODO: Add the constructor code here
			//
			this->game = gcnew Game(TurnPlay::GetHuman());
			this->playedCards = gcnew Dictionary<int, Card^>();
			deck = gcnew Deck();
			cardset = gcnew CardSet();
			for (int i = 0; i < 6; i++)
				cardset->add(this->deck->draw());
			mouseover = -1;
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Panel^  handPanel;
	protected: 

	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::Panel^  handPanel0;
	private: System::Windows::Forms::Panel^  handPanel5;

	private: System::Windows::Forms::Panel^  handPanel4;

	private: System::Windows::Forms::Panel^  handPanel3;

	private: System::Windows::Forms::Panel^  handPanel2;

	private: System::Windows::Forms::Panel^  handPanel1;
	private: System::Windows::Forms::Label^  human_label;
	private: System::ComponentModel::IContainer^  components;



	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
			this->handPanel = (gcnew System::Windows::Forms::Panel());
			this->handPanel7 = (gcnew System::Windows::Forms::Panel());
			this->handPanel6 = (gcnew System::Windows::Forms::Panel());
			this->handPanel5 = (gcnew System::Windows::Forms::Panel());
			this->handPanel4 = (gcnew System::Windows::Forms::Panel());
			this->handPanel3 = (gcnew System::Windows::Forms::Panel());
			this->handPanel2 = (gcnew System::Windows::Forms::Panel());
			this->handPanel1 = (gcnew System::Windows::Forms::Panel());
			this->handPanel0 = (gcnew System::Windows::Forms::Panel());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->human_label = (gcnew System::Windows::Forms::Label());
			this->panel3 = (gcnew System::Windows::Forms::Panel());
			this->clickToCont = (gcnew System::Windows::Forms::Label());
			this->human_cardPanel = (gcnew System::Windows::Forms::Panel());
			this->player4_CardPanel = (gcnew System::Windows::Forms::Panel());
			this->player5_cardPanel = (gcnew System::Windows::Forms::Panel());
			this->player2_cardPanel = (gcnew System::Windows::Forms::Panel());
			this->player3_cardPanel = (gcnew System::Windows::Forms::Panel());
			this->player3_label = (gcnew System::Windows::Forms::Label());
			this->picker_groupBox = (gcnew System::Windows::Forms::GroupBox());
			this->pass_button = (gcnew System::Windows::Forms::Button());
			this->pickup_button = (gcnew System::Windows::Forms::Button());
			this->player2_label = (gcnew System::Windows::Forms::Label());
			this->player5_label = (gcnew System::Windows::Forms::Label());
			this->player4_label = (gcnew System::Windows::Forms::Label());
			this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
			this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->exitToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->handPanel->SuspendLayout();
			this->panel3->SuspendLayout();
			this->picker_groupBox->SuspendLayout();
			this->menuStrip1->SuspendLayout();
			this->SuspendLayout();
			// 
			// handPanel
			// 
			this->handPanel->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
			this->handPanel->Controls->Add(this->handPanel7);
			this->handPanel->Controls->Add(this->handPanel6);
			this->handPanel->Controls->Add(this->handPanel5);
			this->handPanel->Controls->Add(this->handPanel4);
			this->handPanel->Controls->Add(this->handPanel3);
			this->handPanel->Controls->Add(this->handPanel2);
			this->handPanel->Controls->Add(this->handPanel1);
			this->handPanel->Controls->Add(this->handPanel0);
			this->handPanel->Location = System::Drawing::Point(12, 341);
			this->handPanel->Name = L"handPanel";
			this->handPanel->Size = System::Drawing::Size(621, 106);
			this->handPanel->TabIndex = 0;
			this->handPanel->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::panel1_Paint);
			// 
			// handPanel7
			// 
			this->handPanel7->Location = System::Drawing::Point(542, 3);
			this->handPanel7->Name = L"handPanel7";
			this->handPanel7->Size = System::Drawing::Size(71, 96);
			this->handPanel7->TabIndex = 7;
			this->handPanel7->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::handPanel_paint);
			this->handPanel7->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::handPanel_MouseDown);
			// 
			// handPanel6
			// 
			this->handPanel6->Location = System::Drawing::Point(465, 3);
			this->handPanel6->Name = L"handPanel6";
			this->handPanel6->Size = System::Drawing::Size(71, 96);
			this->handPanel6->TabIndex = 6;
			this->handPanel6->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::handPanel_paint);
			this->handPanel6->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::handPanel_MouseDown);
			// 
			// handPanel5
			// 
			this->handPanel5->Location = System::Drawing::Point(388, 3);
			this->handPanel5->Name = L"handPanel5";
			this->handPanel5->Size = System::Drawing::Size(71, 96);
			this->handPanel5->TabIndex = 5;
			this->handPanel5->MouseLeave += gcnew System::EventHandler(this, &Form1::handPanel_MouseLeave);
			this->handPanel5->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::handPanel_paint);
			this->handPanel5->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::handPanel_MouseDown);
			this->handPanel5->MouseEnter += gcnew System::EventHandler(this, &Form1::handPanel_MouseEnter);
			// 
			// handPanel4
			// 
			this->handPanel4->Location = System::Drawing::Point(311, 3);
			this->handPanel4->Name = L"handPanel4";
			this->handPanel4->Size = System::Drawing::Size(71, 96);
			this->handPanel4->TabIndex = 4;
			this->handPanel4->MouseLeave += gcnew System::EventHandler(this, &Form1::handPanel_MouseLeave);
			this->handPanel4->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::handPanel_paint);
			this->handPanel4->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::handPanel_MouseDown);
			this->handPanel4->MouseEnter += gcnew System::EventHandler(this, &Form1::handPanel_MouseEnter);
			// 
			// handPanel3
			// 
			this->handPanel3->Location = System::Drawing::Point(234, 3);
			this->handPanel3->Name = L"handPanel3";
			this->handPanel3->Size = System::Drawing::Size(71, 96);
			this->handPanel3->TabIndex = 3;
			this->handPanel3->MouseLeave += gcnew System::EventHandler(this, &Form1::handPanel_MouseLeave);
			this->handPanel3->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::handPanel_paint);
			this->handPanel3->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::handPanel_MouseDown);
			this->handPanel3->MouseEnter += gcnew System::EventHandler(this, &Form1::handPanel_MouseEnter);
			// 
			// handPanel2
			// 
			this->handPanel2->Location = System::Drawing::Point(157, 3);
			this->handPanel2->Name = L"handPanel2";
			this->handPanel2->Size = System::Drawing::Size(71, 96);
			this->handPanel2->TabIndex = 2;
			this->handPanel2->MouseLeave += gcnew System::EventHandler(this, &Form1::handPanel_MouseLeave);
			this->handPanel2->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::handPanel_paint);
			this->handPanel2->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::handPanel_MouseDown);
			this->handPanel2->MouseEnter += gcnew System::EventHandler(this, &Form1::handPanel_MouseEnter);
			// 
			// handPanel1
			// 
			this->handPanel1->Location = System::Drawing::Point(80, 3);
			this->handPanel1->Name = L"handPanel1";
			this->handPanel1->Size = System::Drawing::Size(71, 96);
			this->handPanel1->TabIndex = 1;
			this->handPanel1->MouseLeave += gcnew System::EventHandler(this, &Form1::handPanel_MouseLeave);
			this->handPanel1->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::handPanel_paint);
			this->handPanel1->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::handPanel_MouseDown);
			this->handPanel1->MouseEnter += gcnew System::EventHandler(this, &Form1::handPanel_MouseEnter);
			// 
			// handPanel0
			// 
			this->handPanel0->Location = System::Drawing::Point(3, 3);
			this->handPanel0->Name = L"handPanel0";
			this->handPanel0->Size = System::Drawing::Size(71, 96);
			this->handPanel0->TabIndex = 0;
			this->handPanel0->MouseLeave += gcnew System::EventHandler(this, &Form1::handPanel_MouseLeave);
			this->handPanel0->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::handPanel_paint);
			this->handPanel0->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::handPanel_MouseDown);
			this->handPanel0->MouseEnter += gcnew System::EventHandler(this, &Form1::handPanel_MouseEnter);
			// 
			// button1
			// 
			this->button1->Enabled = false;
			this->button1->Location = System::Drawing::Point(473, 27);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(154, 29);
			this->button1->TabIndex = 1;
			this->button1->Text = L"Next Game";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// human_label
			// 
			this->human_label->AutoSize = true;
			this->human_label->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 11.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->human_label->Location = System::Drawing::Point(9, 320);
			this->human_label->Name = L"human_label";
			this->human_label->Size = System::Drawing::Size(46, 18);
			this->human_label->TabIndex = 2;
			this->human_label->Text = L"label1";
			// 
			// panel3
			// 
			this->panel3->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
			this->panel3->Controls->Add(this->clickToCont);
			this->panel3->Controls->Add(this->human_cardPanel);
			this->panel3->Controls->Add(this->player4_CardPanel);
			this->panel3->Controls->Add(this->player5_cardPanel);
			this->panel3->Controls->Add(this->player2_cardPanel);
			this->panel3->Controls->Add(this->player3_cardPanel);
			this->panel3->Location = System::Drawing::Point(94, 53);
			this->panel3->Name = L"panel3";
			this->panel3->Size = System::Drawing::Size(302, 260);
			this->panel3->TabIndex = 3;
			this->panel3->MouseClick += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::panel3_MouseClick);
			// 
			// clickToCont
			// 
			this->clickToCont->AutoSize = true;
			this->clickToCont->Location = System::Drawing::Point(102, 105);
			this->clickToCont->Name = L"clickToCont";
			this->clickToCont->Size = System::Drawing::Size(97, 13);
			this->clickToCont->TabIndex = 5;
			this->clickToCont->Text = L"<click to continue>";
			this->clickToCont->Visible = false;
			this->clickToCont->MouseClick += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::panel3_MouseClick);
			// 
			// human_cardPanel
			// 
			this->human_cardPanel->Location = System::Drawing::Point(114, 157);
			this->human_cardPanel->Name = L"human_cardPanel";
			this->human_cardPanel->Size = System::Drawing::Size(71, 96);
			this->human_cardPanel->TabIndex = 0;
			this->human_cardPanel->Tag = L"human";
			this->human_cardPanel->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::paint_cardPanel);
			// 
			// player4_CardPanel
			// 
			this->player4_CardPanel->Location = System::Drawing::Point(224, 4);
			this->player4_CardPanel->Name = L"player4_CardPanel";
			this->player4_CardPanel->Size = System::Drawing::Size(71, 96);
			this->player4_CardPanel->TabIndex = 3;
			this->player4_CardPanel->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::paint_cardPanel);
			// 
			// player5_cardPanel
			// 
			this->player5_cardPanel->Location = System::Drawing::Point(224, 106);
			this->player5_cardPanel->Name = L"player5_cardPanel";
			this->player5_cardPanel->Size = System::Drawing::Size(71, 96);
			this->player5_cardPanel->TabIndex = 4;
			this->player5_cardPanel->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::paint_cardPanel);
			// 
			// player2_cardPanel
			// 
			this->player2_cardPanel->Location = System::Drawing::Point(3, 105);
			this->player2_cardPanel->Name = L"player2_cardPanel";
			this->player2_cardPanel->Size = System::Drawing::Size(71, 96);
			this->player2_cardPanel->TabIndex = 1;
			this->player2_cardPanel->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::paint_cardPanel);
			// 
			// player3_cardPanel
			// 
			this->player3_cardPanel->Location = System::Drawing::Point(3, 3);
			this->player3_cardPanel->Name = L"player3_cardPanel";
			this->player3_cardPanel->Size = System::Drawing::Size(71, 96);
			this->player3_cardPanel->TabIndex = 2;
			this->player3_cardPanel->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::paint_cardPanel);
			// 
			// player3_label
			// 
			this->player3_label->Anchor = System::Windows::Forms::AnchorStyles::Right;
			this->player3_label->Location = System::Drawing::Point(6, 59);
			this->player3_label->Name = L"player3_label";
			this->player3_label->RightToLeft = System::Windows::Forms::RightToLeft::No;
			this->player3_label->Size = System::Drawing::Size(76, 14);
			this->player3_label->TabIndex = 6;
			this->player3_label->Text = L"computer";
			this->player3_label->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
			// 
			// picker_groupBox
			// 
			this->picker_groupBox->Controls->Add(this->pass_button);
			this->picker_groupBox->Controls->Add(this->pickup_button);
			this->picker_groupBox->Enabled = false;
			this->picker_groupBox->Location = System::Drawing::Point(402, 260);
			this->picker_groupBox->Name = L"picker_groupBox";
			this->picker_groupBox->Size = System::Drawing::Size(228, 53);
			this->picker_groupBox->TabIndex = 10;
			this->picker_groupBox->TabStop = false;
			this->picker_groupBox->Text = L"Picker";
			// 
			// pass_button
			// 
			this->pass_button->Enabled = false;
			this->pass_button->Location = System::Drawing::Point(87, 19);
			this->pass_button->Name = L"pass_button";
			this->pass_button->Size = System::Drawing::Size(75, 23);
			this->pass_button->TabIndex = 1;
			this->pass_button->Text = L"Pass";
			this->pass_button->UseVisualStyleBackColor = true;
			this->pass_button->Click += gcnew System::EventHandler(this, &Form1::pass_button_Click);
			// 
			// pickup_button
			// 
			this->pickup_button->Location = System::Drawing::Point(6, 19);
			this->pickup_button->Name = L"pickup_button";
			this->pickup_button->Size = System::Drawing::Size(75, 23);
			this->pickup_button->TabIndex = 0;
			this->pickup_button->Text = L"Pick Up";
			this->pickup_button->UseVisualStyleBackColor = true;
			this->pickup_button->Click += gcnew System::EventHandler(this, &Form1::pickup);
			// 
			// player2_label
			// 
			this->player2_label->Anchor = System::Windows::Forms::AnchorStyles::Right;
			this->player2_label->Location = System::Drawing::Point(6, 160);
			this->player2_label->Name = L"player2_label";
			this->player2_label->RightToLeft = System::Windows::Forms::RightToLeft::No;
			this->player2_label->Size = System::Drawing::Size(76, 14);
			this->player2_label->TabIndex = 11;
			this->player2_label->Text = L"computer";
			this->player2_label->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
			// 
			// player5_label
			// 
			this->player5_label->ForeColor = System::Drawing::SystemColors::ControlText;
			this->player5_label->ImageAlign = System::Drawing::ContentAlignment::MiddleLeft;
			this->player5_label->Location = System::Drawing::Point(399, 139);
			this->player5_label->Name = L"player5_label";
			this->player5_label->RightToLeft = System::Windows::Forms::RightToLeft::No;
			this->player5_label->Size = System::Drawing::Size(76, 14);
			this->player5_label->TabIndex = 12;
			this->player5_label->Text = L"computer";
			this->player5_label->TextAlign = System::Drawing::ContentAlignment::MiddleLeft;
			// 
			// player4_label
			// 
			this->player4_label->ImageAlign = System::Drawing::ContentAlignment::MiddleLeft;
			this->player4_label->Location = System::Drawing::Point(399, 59);
			this->player4_label->Name = L"player4_label";
			this->player4_label->RightToLeft = System::Windows::Forms::RightToLeft::No;
			this->player4_label->Size = System::Drawing::Size(76, 14);
			this->player4_label->TabIndex = 13;
			this->player4_label->Text = L"computer";
			this->player4_label->TextAlign = System::Drawing::ContentAlignment::MiddleLeft;
			// 
			// timer1
			// 
			this->timer1->Enabled = true;
			this->timer1->Interval = 250;
			this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
			// 
			// richTextBox1
			// 
			this->richTextBox1->Location = System::Drawing::Point(473, 62);
			this->richTextBox1->Name = L"richTextBox1";
			this->richTextBox1->ReadOnly = true;
			this->richTextBox1->Size = System::Drawing::Size(154, 189);
			this->richTextBox1->TabIndex = 14;
			this->richTextBox1->Text = L"";
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->fileToolStripMenuItem});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(644, 24);
			this->menuStrip1->TabIndex = 15;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// fileToolStripMenuItem
			// 
			this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->exitToolStripMenuItem});
			this->fileToolStripMenuItem->Name = L"fileToolStripMenuItem";
			this->fileToolStripMenuItem->Size = System::Drawing::Size(37, 20);
			this->fileToolStripMenuItem->Text = L"File";
			// 
			// exitToolStripMenuItem
			// 
			this->exitToolStripMenuItem->Name = L"exitToolStripMenuItem";
			this->exitToolStripMenuItem->Size = System::Drawing::Size(152, 22);
			this->exitToolStripMenuItem->Text = L"Exit";
			this->exitToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::exitToolStripMenuItem_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(644, 459);
			this->Controls->Add(this->richTextBox1);
			this->Controls->Add(this->player4_label);
			this->Controls->Add(this->player5_label);
			this->Controls->Add(this->player2_label);
			this->Controls->Add(this->picker_groupBox);
			this->Controls->Add(this->player3_label);
			this->Controls->Add(this->panel3);
			this->Controls->Add(this->human_label);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->handPanel);
			this->Controls->Add(this->menuStrip1);
			this->Icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));
			this->MainMenuStrip = this->menuStrip1;
			this->Name = L"Form1";
			this->Text = L"Sheepshead";
			this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &Form1::Form1_FormClosing);
			this->handPanel->ResumeLayout(false);
			this->panel3->ResumeLayout(false);
			this->panel3->PerformLayout();
			this->picker_groupBox->ResumeLayout(false);
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

	private: System::Void panel1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {

			 }
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 cardset = gcnew CardSet();
				 //this->game->reset();
				 Diagnostics::Debug::WriteLine("Caught button click!");
				 this->game->Reset();
				 for (int i = 1; i < 5; i++) {
					 nameTags[i-1]->Text = TurnPlay::GetPlayerAt(i)->name;
					 setLabelText(nameTags[i-1], TurnPlay::GetPlayerAt(i)->name);
				 }
				 this->waiting = false;
				 this->Refresh();
					this->Invoke( gcnew SetPicking(this, &Form1::setEnabled), false );
			 }

	private: System::Void handPanel_paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
				 Panel^ panel = (Panel^) sender;
				 int index = panel->TabIndex;
				 CardSet^ hand = this->game->getHand(TurnPlay::GetHuman());
				 //human_label->Text = "Your hand rating is: " + hand->GetStrength();
				 Card^ card = hand->get(index);
				 bool flag = index != mouseover;
				 if (card == nullptr) {
					 return;
				 }
				 Image^ img = CardImage::getImage(card, flag);
				 e->Graphics->DrawImage(img, 0, 0);
			 }
	private: System::Void handPanel_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
				 Panel^ panel = (Panel^) sender;
				 int index = panel->TabIndex;
				 if (this->waiting)
					 return;
				 //this->human_label->Text = "";
				 if (!TurnPlay::IsHumanTurn()) {
					return;
				 }
				 CardSet^ hand = this->game->getHand(TurnPlay::GetHuman());
				 Card^ card = hand->get(index);
				 if (card != nullptr) {
					 if (this->game->hasTrick())
					 {
						Card^ lead = this->game->getTrick()->getLead();
						if (this->game->getHand(TurnPlay::GetHuman())->canFollowSuit(lead)) {
							if (!lead->onSuit(card)) {
								this->human_label->Text = "You have to follow suit!";
								return;
							}
						}
					 }
					 this->game->play(card);
					this->Refresh();
					//this->game->finishTrick();
					//this->Refresh();
					//this->waiting = true;
					//this->human_label->Text = "Click on the Trick above to continue.";
				 }
				
			 }
	private: System::Void handPanel_MouseEnter(System::Object^  sender, System::EventArgs^  e) {
				 Panel^ panel = (Panel^) sender;
				 int index = panel->TabIndex;
				 mouseover = index;
				 panel->Refresh();
			 }
private: System::Void handPanel_MouseLeave(System::Object^  sender, System::EventArgs^  e) {
			 //EventQ::Push(gcnew Event(this, EventArgs::Empty, "test"));
			 Panel^ panel = (Panel^) sender;
			 CardImage^ img = gcnew CardImage(Deck::QUEEN_OF_CLUBS);
			 mouseover = -1;
			 panel->Refresh();
		 }
private: System::Void paint_cardPanel(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
			 if (!this->game->hasTrick())
				 return;
			 Panel^ panel = (Panel^) sender;
			 int index = panel->TabIndex;
			 Player^ p = TurnPlay::GetPlayerAt(index);

			 if (this->game->hasPlayed(index)) {

				 Card^ card = this->game->getCard(index);

				 Image^ img = CardImage::getImage(card, 1);
				 e->Graphics->DrawImage(img, 0, 0);

			 }
			 if (p != nullptr && this->game->hasTrick()) {
				 bool isowner = this->game->getTrick()->getOwner()->Equals(p);
				 bool isleader = this->game->getTrick()->getLeader()->Equals(p);
				 Color c;
				 if (isowner && isleader)
					 c = Color::Purple;
				 else if (isowner)
					 c = Color::Chartreuse;
				 else if (isleader)
					 c = Color::Red;
				 Pen ^pen = gcnew Pen(c, 2.0);
				 e->Graphics->DrawRectangle(pen, 1,1, panel->Width-2,panel->Height - 2);
			 }
			 /*if (this->playedCards->ContainsKey(index)) {
				Image^ img = CardImage::getImage(card, flag);
			 }*/
		 }
private: System::Void panel3_MouseClick(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
			 pcs->Fire("humanq", nullptr, nullptr);
			 clickToCont->Visible = false;
		 }
private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
			 if (repaint_now) {
				System::Diagnostics::Debug::WriteLine("Dispatching repaint request!");
				this->Refresh();
				repaint_now = false;
			 }
		 }
private: System::Void Form1_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
			 pcs->Fire("sigterm", nullptr, nullptr);
			 Application::ExitThread();
			 Application::Exit();
		 }
private: System::Void pickup(System::Object^  sender, System::EventArgs^  e) {
			 setPicking(false);
			 //this->game->Pickup(TurnPlay::GetHuman());
			 this->game->HumanPick();
			 this->Refresh();
		 }
		 

private: System::Void pass_button_Click(System::Object^  sender, System::EventArgs^  e) {
			 setPicking(false);
			 this->game->HumanPass();
			 this->Refresh();
		 }
private: System::Void exitToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->pcs->Fire("sigterm", nullptr, nullptr);
			 Application::ExitThread();
			 Application::Exit();
		 }
};
}

